package com.hdoit.kylin.logical.user.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.kylin.logical.user.dao.ITeacherStudentDao;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;

@Component
public class TeacherStudentDaoImpl extends SuperDao<TeacherStudent> implements
		ITeacherStudentDao {

	@Override
	public List<TeacherStudent> queryList(TeacherStudentSearch search) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		finderExpression.select("TeacherStudent.*")
				.from(TeacherStudent.class, "TeacherStudent")
				.leftJoin(Teacher.class, "teacher")
				.on("TeacherStudent.teacherId", "teacher.employeeId")
				.leftJoin(Student.class, "student")
				.on("TeacherStudent.studentId", "student.employeeId")
				.addOrder("student.admissionDate", OrderEnum.desc)
				.setMainTableAlias("TeacherStudent");
		appendFinder(search, finderExpression);

		return queryList(finderExpression, search);
	}

	private void appendFinder(TeacherStudentSearch search,
			IFinderExpression finderExpression) {
		if (search.getGraduateMark() != -1) {
			finderExpression.andWhere(
					"TeacherStudent.graduateMark = #{graduateMark}").setParam(
					"graduateMark", search.getGraduateMark());
		}

		if (search.getStudentId() != null) {
			finderExpression
					.andWhere("TeacherStudent.studentId = #{studentId}")
					.setParam("studentId", search.getStudentId());
		} else if (search.getTeacherId() != null) {
			finderExpression
					.andWhere("TeacherStudent.teacherId = #{teacherId}")
					.setParam("teacherId", search.getTeacherId());
		}
	}

	@Override
	public int queryStudentsCount(TeacherStudentSearch search) {
		// TODO Auto-generated method stub
		return 0;
	}

}
